<template>
  <el-select
    ref="select"
    v-model="content"
    filterable
    :disabled="disabled"
    remote
    multiple
    clearable
    default-first-option
    placeholder="请输入名称搜索"
    :remote-method="remoteMethod"
    :loading="loading"
    v-bind="$attrs"
  >
    <el-option
      v-for="item in list"
      :key="item.oldFacilitiyNo"
      :label="item.oldFacilitiyName"
      :value="item.oldFacilitiyNo"
    >
      <!-- 自定义选择显示值 -->
      <template>
        <div class="itemBox">
          <span>{{ item.oldFacilitiyName }} </span>
          <!-- <span>{{ item.oldFacilitiyNo }}</span> -->
        </div>
      </template>
    </el-option>
  </el-select>
</template>

<script>
/*
选择XXX-多选

import selectOldFacilityMulti from '@/components/xm/selectOldFacility/selectOldFacilityMulti'

<selectOldFacilityMulti v-model="model.accountNo" />

*/
import { postAction } from '@/api/manage'

export default {
  components: {},
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: Array,
      default: () => []
    },
    param: {
      type: Object,
      default: () => {}
    },
    addData: {
      type: Array,
      default: () => []
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      initList: [],
      list: [],
      newAddData: this.addData
    }
  },
  computed: {
    content: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('change', val)
      }
    }
  },
  mounted() {
    this.getList('', 1)
  },
  methods: {
    // 获取列表
    getList(name, isInitLoad) {
      this.loading = true
      const url = '/old/oldFacility/listPage'
      postAction(url, { oldFacilitiyName: name, pageSize: 50, pageNum: 1, ...this.param }).then(res => {
        if (res.status === 0) {
          this.list = res.data.list || []
          this.list = [...this.list, ...this.newAddData].filter((item, index, Array) => Array.findIndex(i => i.oldFacilitiyNo === item.oldFacilitiyNo) === index)
          if (isInitLoad) {
            this.initList = res.data.list || []
            this.initList = [...this.initList, ...this.newAddData].filter((item, index, Array) => Array.findIndex(i => i.oldFacilitiyNo === item.oldFacilitiyNo) === index)
          }
        }
      }).finally(() => {
        this.loading = false
      })
    },
    // 远程搜索
    remoteMethod(name) {
      this.newAddData = []
      this.getList(name)
    }
  }
}

</script>
<style scoped lang='scss'>
.el-select{
  width:100%;
}
.itemBox{
  display:flex;
  justify-content:space-between;
}
</style>
